﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OBDUDSUtils.CNNMode;
using OBDUDSUtils.Common;

namespace DeviceTestStand
{
    public partial class FrmPlatformsManager : Form
    {
        private List<PlatformMessage> platformData;
        private string dataFile;
        public FrmPlatformsManager(string platDatafile)
        {
            InitializeComponent();
            this.dataFile = platDatafile;
        }

        private void FrmPlatformsManager_Load(object sender, EventArgs e)
        {
            Init();

            this.btn_add.Click += Btn_add_Click;
            this.btn_del.Click += Btn_del_Click;
            this.btn_delAll.Click += Btn_delAll_Click;
            this.btn_update.Click += Btn_update_Click;
            this.btn_close.Click += Btn_close_Click;
        }

        private void Btn_update_Click(object sender, EventArgs e)
        {
            if (UpdatePlatform.WritePlatformMessage(this.dataFile, this.platformData) > 0)
            {
                MessageBox.Show("已完成保存！");
            }
        }

        private void Btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Btn_delAll_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show($"确认要删除所有平台信息？", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
                return;
            this.platformData.Clear();
            this.uiDataGridView1.Rows.Clear();
        }

        private void Btn_del_Click(object sender, EventArgs e)
        {
            var index = this.uiDataGridView1.CurrentRow.Index;
            if (index < 0)
            {
                return;
            }
            var platform = this.uiDataGridView1.CurrentRow.Cells[1].Value.ToString();
            var platformObj = this.platformData.Find(p => p.Platforms == platform);
            if (platformObj == null)
                return;
            if (MessageBox.Show($"确认要删除平台{platform}？", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
                return;
            this.platformData.Remove(platformObj);
            this.uiDataGridView1.Rows.Remove(this.uiDataGridView1.CurrentRow);
        }

        private void Btn_add_Click(object sender, EventArgs e)
        {
            FrmAddPlatform add = new FrmAddPlatform(this.platformData);
            if (add.ShowDialog() == DialogResult.OK)
            {
                AddRow(add.currentPlatform);
            }
        }

        private void Init()
        {
            this.uiDataGridView1.Columns[0].ReadOnly = true;
            this.uiDataGridView1.Columns[1].ReadOnly = true;
            this.uiDataGridView1.Columns[2].ReadOnly = false;
            this.uiDataGridView1.Columns[3].ReadOnly = false;
            this.uiDataGridView1.Columns[4].ReadOnly = true;

            this.uiDataGridView1.AllowUserToAddRows = false;
            this.uiDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.uiDataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            var scriptCol = (DataGridViewComboBoxColumn)this.uiDataGridView1.Columns[2];
            var scriptDir = System.Environment.CurrentDirectory + "\\data\\";
            foreach (var script in new System.IO.DirectoryInfo(scriptDir).GetFiles())
            {
                scriptCol.Items.Add(script.Name);
            }

            this.platformData = new List<PlatformMessage>();
            this.platformData = UpdatePlatform.ReadPlatformMessage(this.dataFile);
            foreach (var msg in this.platformData)
            {
                AddRow(msg);
            }
        }

        private void AddRow(PlatformMessage msg)
        {
            int iRow = this.uiDataGridView1.Rows.Add();
            this.uiDataGridView1.Rows[iRow].Cells[0].Value = iRow + 1;
            this.uiDataGridView1.Rows[iRow].Cells[1].Value = msg.Platforms;
            var frameType = (DataGridViewComboBoxColumn)this.uiDataGridView1.Columns[2];
            var scritpIndex = GetScriptIndex(msg.ScriptName, frameType);
            if (scritpIndex >= 0)
            {
                this.uiDataGridView1.Rows[iRow].Cells[2].Value = frameType.Items[scritpIndex].ToString();
            }
            this.uiDataGridView1.Rows[iRow].Cells[3].Value = msg.SelectSafeVerify;
            this.uiDataGridView1.Rows[iRow].Cells[4].Value = msg.UpdateDate;
        }

        private int GetScriptIndex(string script, DataGridViewComboBoxColumn frameType)
        {

            for(int i = 0;i < frameType.Items.Count;i++)
            {
                if (script.Equals(frameType.Items[i].ToString()))
                    return i;
            }
            return -1;
        }
    }
}
